<?php

if (! defined('LIB_PATH'))
{
    die('The file ' . __FILE__ . ' needs to be required from the main executable of PhpPhoenix');
}

/**
 * Storage 
 * 
 * @package php-phoenix
 * @subpackage core
 * @version $id$
 * @copyright 
 * @author Pablo López Torres <pablolopeztorres@gmail.com> 
 * @license 
 */
class Storage
{

    /**
     * docPath 
     * 
     * @static
     * @var mixed
     * @access public
     */
    public static $docPath = DOC_PATH;

    /**
     * save
     * 
     * @param mixed $data Data to be saved
     * @param string $persistentKey Persistent identifier
     * @param boolean $forceOverwrite If TRUE overwrites the storage cell
     * @static
     * @author Pablo López Torres <pablolopeztorres@gmail.com> 
     * @access public
     * @return boolean Success
     */
    public static function save($data, $persistentKey, $forceOverwrite = FALSE)
    {
        $file = self::getFileForPersistentKey($persistentKey);

        $hasSaved = FALSE;
        if ($forceOverwrite || ! file_exists($file))
        {
            $dirName = dirname($file);

            // if the directory does not exist
            if (! is_dir($dirName)) {

                // try to create it or throw an exception if is not allowed
                if (! mkdir($dirName, 0777, FALSE))
                {
                    $errorText = "Ups! We couln't autocreate the expected dirname: '$dirName' \n";
                    $errorText .= "for the storage key '$persistentKey'. Do I have write permissions to the parent \n";
                    $errorText .= "of this directory?";

                    throw new Exception($errorText);
                }
            }
            file_put_contents($file, serialize($data));
            $hasSaved = TRUE;
        }

        return $hasSaved;
    }

    /**
     * delete
     * 
     * @param string $persistentKey Persistent identifier
     * @param boolean $ignoreMiss If TRUE ignores the KeyNotFoundException
     * @static
     * @author Pablo López Torres <pablolopeztorres@gmail.com> 
     * @access public
     * @return boolean Success
     */
    public static function delete($persistentKey, $ignoreMiss = FALSE)
    {
        $file = self::getFileForPersistentKey($persistentKey);

        $hasSaved = FALSE;
        if (file_exists($file))
        {
            unlink($file);
            $hasSaved = TRUE;
		}
		else
		{
			if ($ignoreMiss === FALSE)
			{
				throw new KeyNotFoundException();
			}
		}

        return $hasSaved;
    }

    /**
     * load
     * 
     * @param string $persistentKey Persistent identifier
     * @static
     * @author Pablo López Torres <pablolopeztorres@gmail.com> 
     * @access public
     * @return mixed
     */
    public static function load($persistentKey)
    {
        $file = self::getFileForPersistentKey($persistentKey);

        $element = NULL;
        if (file_exists($file))
        {
            $element = unserialize(file_get_contents($file));
        }

        return $element;
    }

    /**
     * getFileForPersistentKey 
     * 
     * @param mixed $persistentKey 
     * @static
     * @author Pablo López Torres <pablolopeztorres@gmail.com> 
     * @access private
     * @return void
     */
    private static function getFileForPersistentKey($persistentKey)
    {
        return self::$docPath . $persistentKey . '~';
    }
}


class KeyNotFoundException extends Exception
{
	
}
